package com.crm.controller;

import com.crm.entity.CusDevPlan;
import com.crm.entity.SaleChance;
import com.crm.service.CusDevPlanService;
import com.crm.service.SaleChanceService;
import com.crm.util.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户开发计划Controller层
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/cusDevPlan")
public class CusDevPlanController {

    @Resource
    private CusDevPlanService cusDevPlanService;

    @Resource
    private SaleChanceService saleChanceService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);   //进行日期计算，比如55个月，就是4年后。
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   //true:允许输入空值，false:不能为空值
    }

    /**
     * 分页条件查询客户开发计划
     *
     * @param saleChanceId
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    public String list(@RequestParam(value = "saleChanceId",required=false) String saleChanceId, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("saleChanceId", saleChanceId);
        List<CusDevPlan> cusDevPlanList = cusDevPlanService.find(map);
        JSONObject result = new JSONObject();
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(new String[]{"saleChance"});
        jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
        JSONArray jsonArray = JSONArray.fromObject(cusDevPlanList, jsonConfig);
        result.put("rows", jsonArray);
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 添加或者修改客户开发计划
     *
     * @param cusDevPlan
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    public String save(CusDevPlan cusDevPlan, HttpServletResponse response) throws Exception {
        int resultTotal = 0; // 操作的记录条数
        if (cusDevPlan.getId() == null) {
            SaleChance saleChance = new SaleChance();
            saleChance.setId(cusDevPlan.getSaleChance().getId());
            saleChance.setDevResult(1); // 状态修改成"开发中"
            saleChanceService.update(saleChance);
            resultTotal = cusDevPlanService.add(cusDevPlan);
        } else {
            resultTotal = cusDevPlanService.update(cusDevPlan);
        }
        JSONObject result = new JSONObject();
        if (resultTotal > 0) {
            result.put("success", true);
        } else {
            result.put("success", false);
        }
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 删除客户开发计划
     *
     * @param id
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete")
    public String delete(@RequestParam(value = "id") String id, HttpServletResponse response) throws Exception {
        cusDevPlanService.delete(Integer.parseInt(id));
        JSONObject result = new JSONObject();
        result.put("success", true);       //  result.put("isError", true);
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 修改客户开发状态
     *
     * @param id
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateSaleChanceDevResult")
    public String updateSaleChanceDevResult(@RequestParam(value = "id") String id, @RequestParam(value = "devResult") String devResult, HttpServletResponse response) throws Exception {
        SaleChance saleChance = new SaleChance();
        saleChance.setId(Integer.parseInt(id));
        saleChance.setDevResult(Integer.parseInt(devResult));
        int resultTotal = saleChanceService.update(saleChance);
        JSONObject result = new JSONObject();
        if (resultTotal > 0) {
            result.put("success", true);
        } else {
            result.put("success", false);
        }
        ResponseUtil.write(response, result);
        return null;
    }
}
